﻿using System;
using System.IO;
using System.Net;
using System.Linq;
using System.Text;
using System.Activities;
using System.Activities.Expressions;
using System.Activities.Presentation.PropertyEditing;
using System.Activities.Statements;
using System.Collections.Generic;
using System.ComponentModel;

namespace Vision.Core.ActivityComponents.SharePoint
{
    /// <summary>
    /// 
    /// </summary>    
    [System.ComponentModel.Designer(typeof(SPListServiceDesigner))]
    public class SPListService : NativeActivity
    {
        /// <summary>
        /// 
        /// </summary>
        Components.DynamicWebService SharePointSvc { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [Browsable(false)]
        [DefaultValue(null)]
        public Activity Body { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [Category("Input")]
        [DefaultValue("")]
        public InArgument<string> SiteUrl { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [Category("Input")]
        [DefaultValue("")]
        public InArgument<string> SPUserName { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [Category("Input")]
        [DefaultValue("")]
        public InArgument<string> SPUserPassword { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [Category("Input")]
        [DefaultValue("")]
        public InArgument<string> SPUserDomain { get; set; }
    
        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        protected override void Execute(NativeActivityContext context)
        {
            if (this.Body != null){
                this.SharePointSvc = new Components.DynamicWebService();
                this.SharePointSvc.WsdlUrl = this.SiteUrl.Get(context) + "/_vti_bin/Lists.asmx?wsdl";
                this.SharePointSvc.Protocol = Components.DynamicWebService.HttpProtocol.Soap12;
                this.SharePointSvc.Credentials = new System.Net.NetworkCredential(
                    this.SPUserName.Get(context),
                    this.SPUserPassword.Get(context),
                    this.SPUserDomain.Get(context)
                );

                this.SharePointSvc.InitService();

                context.Properties.Add("F1634880-5000-11E0-A748-B487DFD72085", this.SharePointSvc);
                context.ScheduleActivity(this.Body, this.OnChildActivityCompleted);                
            }                        
        }
                
        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="completedInstance"></param>
        private void OnChildActivityCompleted(NativeActivityContext context, ActivityInstance completedInstance)
        {
            context.Properties.Remove("F1634880-5000-11E0-A748-B487DFD72085");
            if (this.SharePointSvc != null){
                this.SharePointSvc.Dispose();
            }
        }
    }
}
